<!DOCTYPE html>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<style>
#tests_container {
  font-size: 20px;
  line-height: 1;
  font-family: monospace;
  width: 2ch;
}
inline-block {
  display: inline-block;
  width: 1ch;
  height: .6em;
  background: pink;
}
</style>
<body>
  <pre id="console"></pre>
  <div id="tests_container"></div>
<script>
let tests_container = document.getElementById('tests_container');
let log_container = document.getElementById('console');

class LineBreakTest {
  constructor(name, innerHTML) {
    this.name = name;
    let element = document.createElement('div');
    element.innerHTML = innerHTML;
    tests_container.appendChild(element);
    this.element = element;
  }

  setWidth(width) {
    this.element.style.width = `${width - 1}px`;
  }

  line_count() {
    let height = this.element.offsetHeight;
    console.assert(height === 20 || height === 40);
    return Math.round(height / 20);
  }
}

function run() {
  // Collects code points to test.
  let code_points = [];
  for (let code_point = 0x21; code_point <= 0x7E; code_point++)
    code_points.push(code_point);
  code_points.push(0xA0);  // NBSP
  code_points.push(0x56FD);  // CJK UNIFIED IDEOGRAPH-56FD 国

  let tests = [];
  for (let code_point of code_points) {
    let name = code_point.toString(16).toUpperCase();
    let str = String.fromCodePoint(code_point);
    tests.push(new LineBreakTest(`Before U+${name}`, '<inline-block></inline-block>' + str));
    tests.push(new LineBreakTest(`After U+${name}`, str + '<inline-block></inline-block>'));
  }

  // Shrink by 1px so that the line does not fit.
  for (let t of tests) {
    console.assert(t.line_count() === 1);
    t.width = t.element.offsetWidth;
  }
  for (let t of tests)
    t.setWidth(t.width - 1);

  // Collect results.
  let logs = [];
  let no_break_count = 0;
  for (let t of tests) {
    if (t.line_count() === 1) {
      ++no_break_count;
      logs.push(`No break ${t.name}`);
    }
  }
  logs.push(`${no_break_count} unbreakable combinations found`);
  log_container.textContent = logs.join('\n');

  assert_equals(no_break_count, 0);
}

test(run);
</script>
</body>
